/**
 * Copyright &copy; 2015-2020 <a href="http://www.easyhis.com/">easyhis</a> All rights reserved.
 */
package com.easy.modules.sys.entity;

import com.easy.common.utils.IdGen;
import com.easy.common.utils.Reflections;
import com.easy.common.utils.StringUtils;
import com.easy.core.persistence.BaseEntity;
import com.easy.modules.sys.utils.UserUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.validator.constraints.Length;

import java.util.Date;

/**
 * 区域Entity
 * @author easy
 * @version 2016-05-15
 */
public class Area extends BaseEntity<Area> {


	private static final long serialVersionUID = 1L;
	private String id;
	private Integer areaId;
	private Area parent;	// 父级编号
	private String parentName; 	// 父级名称
	private String areaName; 	// 名称
	private String areaCode; 	// 区域编码
	private String areaType; 	// 区域类型（1：国家；2：省份、直辖市；3：地市；4：区县）
	private String pyCode; 	// 区域编码
	private String zipCode; 	// 区域类型（1：国家；2：省份、直辖市；3：地市；4：区县）
	private String telArea; 	// 电话区号
	private String remarks;	// 备注
	private User createBy;	// 创建者
	private Date createTime;	// 创建日期
	private User updateBy;	// 更新者
	private Date updateTime;	// 更新日期
	private Integer sortNo;		// 排序
	private boolean hasChildren;//是否有子节点
	private Integer delFlag; 	// 删除标记（0：正常；1：删除；2：审核）


	public Area(){
		sortNo = 30;
		this.delFlag = this.DEL_FLAG_NORMAL;
	}

	public Area(Integer areaId){
		this.areaId = areaId;
	}

	public Area(String id){
		this.id = id;
	}

	//	@JsonBackReference
//	@NotNull
	public Area getParent() {
		return parent;
	}

	public void setParent(Area parent) {
		this.parent = parent;
	}

	public Integer getAreaId() {
		return areaId;
	}

	public void setAreaId(Integer areaId) {
		this.areaId = areaId;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getAreaName() {
		return areaName;
	}

	public void setAreaName(String areaName) {
		this.areaName = areaName;
	}

	public String getAreaCode() {
		return areaCode;
	}

	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}

	public String getAreaType() {
		return areaType;
	}

	public void setAreaType(String areaType) {
		this.areaType = areaType;
	}

	public String getPyCode() {
		return pyCode;
	}

	public void setPyCode(String pyCode) {
		this.pyCode = pyCode;
	}

	public String getZipCode() {
		return zipCode;
	}

	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}

	public String getTelArea() {
		return telArea;
	}

	public void setTelArea(String telArea) {
		this.telArea = telArea;
	}

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public User getCreateBy() {
		return createBy;
	}

	public void setCreateBy(User createBy) {
		this.createBy = createBy;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public User getUpdateBy() {
		return updateBy;
	}

	public void setUpdateBy(User updateBy) {
		this.updateBy = updateBy;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public Integer getSortNo() {
		return sortNo;
	}

	public void setSortNo(Integer sortNo) {
		this.sortNo = sortNo;
	}

	public boolean isHasChildren() {
		return hasChildren;
	}

	public void setHasChildren(boolean hasChildren) {
		this.hasChildren = hasChildren;
	}

	@JsonIgnore
	@Length(min=1, max=1)
	public Integer getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(Integer delFlag) {
		this.delFlag = delFlag;
	}

	public String getParentName() {
		return parentName;
	}

	public void setParentName(String parentName) {
		this.parentName = parentName;
	}

	/**
	 * 插入之前执行方法，需要手动调用
	 */
	public void preInsert(){
		this.id = IdGen.uuid();
		User user = UserUtils.getUser();
		if (user.getUserId()<1){
			this.updateBy = user;
			this.createBy = user;
		}
		this.updateTime = new Date();
		this.createTime = this.updateTime;
	}

	/**
	 * 更新之前执行方法，需要手动调用
	 */
	public void preUpdate(){
		User user = UserUtils.getUser();
		if (user.getUserId()<1){
			this.updateBy = user;
		}
		this.updateTime = new Date();
	}

	public String getParentId() {
		String id = null;
		if (parent != null){
			id = (String) Reflections.getFieldValue(parent, "id");
		}
		return StringUtils.isNotBlank(id) ? id : "0";
	}

	public String toString() {
		return areaName;
	}

	@Override
	public boolean equals(Object obj) {
		if (null == obj) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		if (!getClass().equals(obj.getClass())) {
			return false;
		}
		Area that = (Area) obj;
		return null == this.getAreaId() ? false : this.getAreaId()==that.getAreaId();
	}

}